<?php
/*
 * 推荐管理
 */

if(!defined('IN_SITE') || !defined('IN_ADMIN')) {
	exit('Access Denied');
}

$op = in_array($op, array("list", "add", "addsave", "del", "edit", "editsave")) ? $op : 'list';

switch ($op) {
	case 'list':

	$lid = $_GET['lid'];  //得到显示位置id
	$lname = $_GET['lname']; //得到显示位置名称
	
	if(!$lid) showMessage("缺少参数！");
	$sql = '';
	$sql="SELECT id, lid, subject, summary, img, url, pubtime FROM {$tablepre}recommend WHERE lid='$lid' ORDER BY pubtime DESC";

	//列出该ID下所有推荐条目
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$recommend[] = $row;
	}
	break;
	
	case 'add':
		
	$lid = $_GET['lid'];
	$lname = $_GET['lname'];  //得到显示位置
	$fontnum = $_GET['f'];
	$width = $_GET['w'];
	$height = $_GET['h'];

	break;
	
	case "addsave":
			
	if($_POST['Submitted'] && !empty($lid) && !empty($subject) && !empty($url)){
		$subject = trim($_POST['subject']);
		$summary = trim($_POST['summary']);
		$url = trim($_POST['url']);
		$lid = $_POST['lid'];

		//上传图片
		require_once SITE_ROOT.'include/upload.class.php';
		if($_FILES['uploadimg']['tmp_name']) {
			$dir = date('y').'/'.date('m');
			$savePath = SITE_ROOT."attachments/".$dir;
			$f = new upload("uploadimg", $savePath);
			$f->up();
			$img = $f->uploadedfiles;
			$imgPath = $img[0]['filepath'];
		}
				
		$query = $db->query("INSERT INTO {$tablepre}recommend(lid, subject, summary, img, url, pubtime)
			 VALUES('$lid', '$subject', '$summary', '$imgPath', '$url', '$timestamp')");
		if($query) showMessage("添加成功！", "?action=recommend&op=list&lname=$lname&lid=$lid");
	}
	break;
	
	case 'edit';
	
	$id = $_GET['id'];
	//取该ID数据
	$query = $db->query("SELECT id, lid, subject, summary, img, url, pubtime FROM {$tablepre}recommend WHERE id='$id'");
	$recommend= $db->fetch_array($query);
	break;
	
	case 'editsave';
	$id = $_POST['id'];
	if($_POST['Submitted'] && !empty($id) && !empty($subject) && !empty($url)){
		$subject = trim($_POST['subject']);
		$summary = trim($_POST['summary']);
		$url = trim($_POST['url']);

		//上传图片
		if($_FILES['uploadimg']['tmp_name']){
			require_once SITE_ROOT.'include/upload.class.php';
			$dir = date('y').'/'.date('m');
			$savePath = SITE_ROOT."attachments/".$dir;
			$f = new upload("uploadimg", $savePath);
			$f->up();
			$img = $f->uploadedfiles;
			$imgPath = $img[0]['filepath'];

			$query = $db->query("UPDATE {$tablepre}recommend SET img='$imgPath' WHERE id='$id'");
			@unlink(SITE_ROOT.$_POST['img_old']);
		}

		$query = $db->query("UPDATE {$tablepre}recommend SET subject='$subject', summary='$summary', url='$url' WHERE id='$id'");
		if($query) showMessage("修改成功", "?action=recommendlist&op=list");

	}
	break;
	
	case 'del';
	
	$id = $_POST['id'];
	$img = $_POST['img_old'];

	if(isset($id) && !empty($id)) {
		foreach ($id as $k => $v) {
			$query = $db->query("DELETE FROM {$tablepre}recommend WHERE id='$v'");
			@unlink(SITE_ROOT.$img[$v]);
		}
		if($query) showMessage("删除成功！", "?action=recommendlist&op=list");
	}
}
include admin_tpl("recommend");
?>